package player;

import java.util.List;
import java.util.Scanner;

import board.Board;

/*


HumanPlayer::HumanPlayer(int color, string name) {
	m_color = color;
	m_name = name;
}

HumanPlayer::~HumanPlayer() {
	// Nothing yet to do here
}

 * Method which reads the next move from the human (over keyboard) and executed the move if possible.
 * If not, the player will be asked as long until he enters a legal move.

void HumanPlayer::makeNextMove(board* b){
	bool stringValid = true;
	string moveStr;		// Move string
	unsigned int move = 0;		// Move "sequence"
	bool moved = false;			// If the tried move has been made (was legal) or not.
	set<unsigned long long> tempMoveSet,tempMoveSet2;

	/* Ask move until it is a legal one 
	while(!moved || !stringValid){
		stringValid = true;
		b->calculatePossibleMoves(m_color);		// Moves have to be calculated before moving
		tempMoveSet2 = b->getPossibleMoves(m_color);
		std::cout<<"*recently calculated : ";b->printSet(tempMoveSet2);
		cout << "Player '" << m_name << "':" << endl;
		cout << "What will be your move?:  ";
		cin >> moveStr;		// Combine moveFrom and moveTo
		cout << "Move = " << moveStr << endl;

		move = 0;

		/* Check if the sequence is valid
		for(unsigned int i = 0; i<strlen(moveStr.c_str());i++){
			if(((int)moveStr[i])-48<((int)'1')-48 || ((int)moveStr[i])-48>((int)'9')-48 ||
					strlen(moveStr.c_str())<4 || strlen(moveStr.c_str())%2!=0){
				stringValid = false;
			} else {
				move = move*10 + ((int)moveStr[i])-48;
			}
		}

		if(stringValid){
			moved = b->movePiece(move, m_color);	// TRY
		} else {
			cout << "Invalid move sequence!" << endl;

			moveStr.clear();
		}
	}

	b->cleanUndo();
}

string HumanPlayer::getName(){
	return m_name;
}
*/
public class HumanPlayer implements Player {

	private int color;
	private String name;
	
	public HumanPlayer(int color, String name) {
		super();
		this.color = color;
		this.name = name;
	}
	public int getColor() {
		return color;
	}
	public void setColor(int color) {
		this.color = color;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public void makeNextMove(Board  b){
		boolean stringValid = true;
		String moveStr;		// Move string
		int move = 0;		// Move "sequence"
		boolean moved = false;			// If the tried move has been made (was legal) or not.
		List tempMoveSet,tempMoveSet2;

		/* Ask move until it is a legal one */
		while(!moved || !stringValid){
			stringValid = true;
			b.calculatePossibleMoves(getColor());		// Moves have to be calculated before moving
			tempMoveSet2 = b.getPossibleMoves(getColor());
			System.out.println("*recently calculated : ");
			b.printSet(tempMoveSet2);
			System.out.println("Player '" + getName() + "':" );
			System.out.println("What will be your move?:  ");
			Scanner sc = new Scanner(System.in);
			moveStr =sc.next();
			System.out.println("Move = " + moveStr );
			move = 0;

			/* Check if the sequence is valid */
			for(int i = 0; i<moveStr.length();i++){
				if(((int)moveStr.charAt(i))-48<((int)'1')-48 || ((int)moveStr.charAt(i))-48>((int)'9')-48 ||
						moveStr.length()<4 || moveStr.length()%2!=0){
					stringValid = false;
				} else {
					move = move*10 + ((int)moveStr.charAt(i))-48;
				}
			}

			if(stringValid){
				moved = b.movePiece(move, getColor());	// TRY
			} else {
				System.out.println( "Invalid move sequence!");

				moveStr = "";
			}
		}

		//b.cleanUndo();
		b.clean();
	}
}

